<?php
namespace App\Http\Controllers\AdminApi;
use App\Model\LinkTable;
use App\Repository\Admin\AdminService;
use App\Repository\BaseController\ApiController;
use App\Repository\Common\ConstTable;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;
//合作logo管理
class LinksController extends ApiController{
	//合作logo-列表
    public function getLinkList(){
        $page = \Request::get('page', 1);
        $limit = \Request::get('page_num', 10);
        $title = \Request::get('title');
        return JsonRespose::done(LinkTable::getLinkList($page,$limit,$title,0));
    }
    //合作logo-详情
    public function getLinkInfo(){
        $id = \Request::get('id');
        $res = LinkTable::ORM()->where('id', $id)->first();
        if (!$res) {
            return JsonRespose::fail('数据不存在');
        }
        $data = [
            'id' => $res->id,
            'title' => $res->title,
            'img_url' => $res->img_url,
            'link_url' => $res->link_url,
        ];
        return JsonRespose::success($data);
    }
    //合作logo-添加修改
    public function postLinkSave(){
        //获取所有参数到$param数组
        $param = \Request::all();
        $data = [
            'id' => $param['id'],
            'uid' => AdminService::getAdminId(),
            'title' => $param['title'],
            'img_url' => $param['img_url'],
            'link_url' => $param['link_url'],
            'status' => $param['status'],
            'created_at' => Str::getNowDateTime(),
        ];
        //判断是否存在id
        if (isset($param['id']) and $param['id']) {
            //根据id更新数据
            $res = LinkTable::ORM()->where('id', $param['id'])->update($data);
        } else {
            //否则插入数据
            $data['updated_at'] = Str::getNowDateTime();
            $res = LinkTable::ORM()->insertGetId($data);
            //自增id更新为排序id
            LinkTable::ORM()->where('id', $res)->update(['sort' => $res]); //排序id
            $param['id'] = $res;
        }
        if ($res) {
            //接口返回数据
            return JsonRespose::success(['id' => $param['id']]);
        } else {
            return JsonRespose::fail();
        }
    }
    //合作logo-删除
    public function postLinkDel(){
        $id = \Request::get('id');
        //取得当前数据
        $res = LinkTable::ORM()->where('id', $id)->update(['status' => -1]);
        if ($res) {
            return JsonRespose::success();
        } else {
            return JsonRespose::fail();
        }
    }
    //合作logo-刷新上浮
   	public function postLinkUp(){
        $id = \Request::get('id');
        //取得当前数据
        $res = LinkTable::ORM()->where('id', $id)->first();
        //取得上面一条数据
        $res_top = \DB::select('select * from link WHERE sort>? ORDER BY sort ASC limit 1', [$res->sort]);
        //如果是挤到最后,判断不存在时取出最后一条 $res_top
        if (!isset($res_top[0])) {
            $res_top = \DB::select('select * from link ORDER BY sort ASC limit 1', []);
        }
        if (!isset($res_top[0]) or ($res_top[0]->id == $res->id)) {
            return JsonRespose::success();
        }
        //当前数据和上一条数据 做update 操作 desc
        $s1 = LinkTable::ORM()->where('id', $res->id)->update(array('sort' => $res_top[0]->sort));
        $s2 = LinkTable::ORM()->where('id', $res_top[0]->id)->update(array('sort' => $res->sort));
        if ($s1 and $s2) {
            return JsonRespose::success();
        } else {
            return JsonRespose::fail();
        }
    }
    //合作logo-置顶
    public function postLinkTop(){
        $id = \Request::get('id');
        $res = LinkTable::ORM()->where('id', $id)->update(array('sort_top' => 1));
        if ($res) {
            return JsonRespose::success();
        } else {
            return JsonRespose::fail();
        }
    }
    //合作logo-取消置顶 恢复原位
    public function postLinkTopDel(){
        $id = \Request::get('id');
        //取得当前数据
        $res = LinkTable::ORM()->where('id', $id)->update(array('sort_top' => 0));
        if ($res) {
            return JsonRespose::success();
        } else {
            return JsonRespose::fail();
        }
    }
}


